package com.frxuexi.shop.viewmodel

import com.frxuexi.lib_base.http.ShopApiService
import com.frxuexi.lib_base.mvi.BaseViewModel
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.room.ShopDao
import com.frxuexi.shop.ShopDataSource
import com.frxuexi.shop.intent.ShopIntent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class ShopViewModel @Inject constructor(val apiService: ShopApiService,val shopDao: ShopDao):BaseViewModel<ShopIntent>() {
    override fun handleIntent(it: ShopIntent) {
        when(it){
//            is ShopIntent.getShops->httpRequest(apiService.getShops(it.type,it.page))
            is ShopIntent.getShops->pagerRequest {
                ShopDataSource(apiService,it.type)
            }
            is ShopIntent.getShopDetail->{
                httpRequest(apiService.getShopById(it.id))
            }
            is ShopIntent.addLove->{
                roomRequest(flow { emit(shopDao.addLove(it.shopEntity)) },RequestType.UPDATE)
            }
            is ShopIntent.isLove->roomRequest(flow { emit(shopDao.isLove(it.sid)) },RequestType.ITEM)
            is ShopIntent.deleteLove->roomRequest(flow { emit(shopDao.deleteLove(it.shopEntity)) },RequestType.DELETE)
            is ShopIntent.getLoves->roomRequest(flow { emit(shopDao.getLoves()) },RequestType.LOVES)
            is ShopIntent.addCart->httpRequest(apiService.addCart(toBody(it.map)),RequestType.ADD)
            is ShopIntent.getComments->httpRequest(apiService.getComments(it.id,it.page),RequestType.COMMENT)
            else -> {}
        }
    }
}